package com.lsus.golkhadi.client.viewsImpl;


import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.allen_sauer.gwt.dnd.client.drop.AbsolutePositionDropController;
import com.allen_sauer.gwt.dnd.client.drop.DropController;
import com.allen_sauer.gwt.dnd.client.drop.SimpleDropController;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;
import com.lsus.golkhadi.client.Golkhadi;
import com.lsus.golkhadi.client.ICardService;
import com.lsus.golkhadi.client.ICardServiceAsync;
import com.lsus.golkhadi.client.ILoginService;
import com.lsus.golkhadi.client.ILoginServiceAsync;
import com.lsus.golkhadi.client.images.CardToImageMap;
import com.lsus.golkhadi.client.images.GolkhadiImageBundle;
import com.lsus.golkhadi.client.images.Invoke;
import com.lsus.golkhadi.client.views.IMainPanel;
import com.lsus.golkhadi.shared.Card;

public class MainPanel extends Composite implements IMainPanel {
	
	final ICardServiceAsync cardService = ( ICardServiceAsync )GWT.create( ICardService.class );
	
	interface MainViewUiBinder extends UiBinder<Widget, MainPanel> {
	}
	private static MainViewUiBinder uiBinder = GWT.create(MainViewUiBinder.class);
	
	//should have its own service later
	final ILoginServiceAsync loginService = ( ILoginServiceAsync )GWT.create( ILoginService.class );

	@UiField Image player1;
	@UiField Image player2;
	@UiField Image player3;
	@UiField Image player4;
	@UiField Button suffle;
	@UiField Button showCards;
	@UiField Label loginStatus;
	@UiField DockLayoutPanel mainGolkhadiPanel;
	@UiField HorizontalPanel playerCardholder;
	private CenterTable centerTable;
	
	
	public MainPanel(){
		
		initWidget(uiBinder.createAndBindUi(this));
		
		player2.setResource(GolkhadiImageBundle.INSTANCE.Suresh());
		player3.setResource(GolkhadiImageBundle.INSTANCE.Subhash());
		player1.setResource(GolkhadiImageBundle.INSTANCE.Ligaj());
		player4.setResource(GolkhadiImageBundle.INSTANCE.Uttam());
		centerTable = new CenterTable();
		mainGolkhadiPanel.add(centerTable);
		
		//for arranging my cards 
		SetWidgetDropController dropController1 = new SetWidgetDropController(playerCardholder);
		Golkhadi.dragController.registerDropController(dropController1);
		
		//for throwing on the table
		SetWidgetOnTableDropController dropController2 = new SetWidgetOnTableDropController(centerTable, playerCardholder);
		Golkhadi.dragController.registerDropController(dropController2);
		
	}
	
	//suffling the cards
	@UiHandler("suffle")
	void onSuffleClick(ClickEvent e){
		cardService.suffle(LoginPanel.player.getSessionId(), new AsyncCallback<Boolean>() {
			@Override
			public void onSuccess(Boolean result) {
				loginStatus.setText("suffled!");
			}

			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Sorry conneciton problem!");
			}
		});
	}
	
	//suffling the cards
	@UiHandler("showCards")
	void showMyCardsAfterSuffling(ClickEvent e){
		cardService.getMyCards(LoginPanel.player.getSessionId(), new AsyncCallback<List<Card>> () {
			@Override
			public void onSuccess(List<Card> result) {
				playerCardholder.clear();
				Iterator<Card> iterator = result.iterator();
				  while ( iterator.hasNext() ){
				      Card c = (Card)iterator.next();
				      Map<Card, Invoke> m = CardToImageMap.getInstance().getMap();
				      ImageResource imageResource = (ImageResource)m.get(c).imageGetter();
				      CardImage i = new CardImage(imageResource);
				      i.setCard(c);
				      playerCardholder.add(i);
				      Golkhadi.dragController.makeDraggable(i);
				  }
			}

			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Sorry conneciton problem!");
			}
		});
	}
	

}
